﻿<%@ Page Language="C#" %>
<%@ Import Namespace="Backfire" %>
<script runat="server">
  /// <summary>
  /// This is an example implementation of how to implement a Backfire server.
  /// It does no security checks or file existence checks whatsoever.
  /// It is a blunt-axe straightforward example that saves the changes back to the
  /// css files of origin, for illustration purposes.
  /// Enjoy! :-)
  /// </summary>
  public class ExampleImplementation
  {
    public static void VerifyAccessOnLoad()
    {
      Backfire.HQ.SendMessageToClient(PostbackMessage.AccessGranted, HttpContext.Current.Response);
    }

    public static void ProcessChanges()
    {
      string changes = HttpContext.Current.Request.Form["backfire-changes"];
      PostbackMessage result = HQ.Process(changes, LoadCssFromFile, SaveCssToFile);
      HQ.SendMessageToClient(result, HttpContext.Current.Response);
    }

    public static string LoadCssFromFile(string uri)
    {
      string fileName = HttpContext.Current.Server.MapPath(uri.Substring(uri.IndexOf("/Css")));
      return System.IO.File.ReadAllText(fileName);
    }

    public static bool SaveCssToFile(string uri, string contents)
    {
      string fileName = HttpContext.Current.Server.MapPath(uri.Substring(uri.IndexOf("/Css")));
      System.IO.File.WriteAllText(fileName, contents);
      return true;
    }
  }
</script>
<%
  if (Request.HttpMethod == "GET")
    ExampleImplementation.VerifyAccessOnLoad();
  if (Request.HttpMethod == "POST")
    ExampleImplementation.ProcessChanges();
%>